/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logical.allhit.gui;

import com.logical.allhit.http.HTTPClient;
import com.logical.allhit.http.HTTPClientAdd;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Icon;
import javax.swing.JTextField;

/**
 *
 * @author Sanjeet Jha
 */
public class CaptchaImg extends javax.swing.JDialog implements KeyListener {

    public static final int login_image_code = 1;
    public static final int add_image_code = 2;
    private int code;

    /**
     * Creates new form CaptchaImg
     */
    public CaptchaImg(java.awt.Frame parent, boolean modal, String url, int code) {
        super(parent, modal);
        initComponents();
        this.code = code;
        jTextField1.addKeyListener(this);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(d.width / 2 - WIDTH / 2, d.height / 2 - HEIGHT / 2);
        loadImage(url);
    }

    private void loadImage(String url) {

        try {
//            lblImage.setIcon(new javax.swing.ImageIcon(HTTPClient.getInstance().GETIMAGE("http://202.87.47.29/~allhitde/" 
//                    + url /*"captcha_code_file.php?rand=1236681468"*/)));
            if(code == login_image_code){
            lblImage.setIcon(new javax.swing.ImageIcon(HTTPClient.getInstance().GETIMAGE("http://" + com.logical.allhit.setting.Setting.getInstance().getHost() + "/"
                    + url /*"captcha_code_file.php?rand=1236681468"*/)));
            }else if (code == add_image_code){
                lblImage.setIcon(new javax.swing.ImageIcon(HTTPClientAdd.getInstance().GETIMAGE("http://promos.allhitdeals.com/deals/" + url )));
//                lblImage.setIcon(new javax.swing.ImageIcon(HTTPClientAdd.getInstance().GETIMAGE("http://202.87.47.29/~allhitdl/deals/" + url )));
            }
        } catch (Exception ex) {
        }

//        try
//        {
//            java.net.URL turl = new java.net.URL("http", "www.allhitdeals.com", "/"+url);
//            lblImage.setIcon(new javax.swing.ImageIcon(turl)); 
//        }
//        catch(Exception ex)
//        {
//        
//        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblImage = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel2.setText("Type Image Code : ");

        jTextField1.setName(""); // NOI18N

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblImage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblImage, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        submitCode();
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JLabel lblImage;
    // End of variables declaration//GEN-END:variables

    @Override
    public void keyTyped(KeyEvent e) {
        
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        JTextField field = (JTextField) e.getSource();
        if(field.equals(jTextField1) && e.getKeyCode() == KeyEvent.VK_ENTER){
            submitCode();
        }
    }
    
    private void submitCode(){
        if (code == login_image_code) {
            HTTPClient.getInstance().setImageCode(jTextField1.getText().trim());
        } else if (code == add_image_code) {
            HTTPClientAdd.getInstance().setImageCode(jTextField1.getText().trim());
        }
        setVisible(false);
    }
}
